<template>
  <div class="order-container">
    <div v-if="isLoggedIn" class="user-orders">
      <h2>您的订单</h2>
      <div v-for="order in orders" :key="order.id" class="order-item">
        <div class="order-info">
          <p><strong>订单号:</strong> {{ order.id }}</p>
          <p><strong>总价:</strong> ¥{{ order.totalPrice }}</p>
          <p><strong>支付状态:</strong> <span class="status-tag">{{ order.paymentStatus }}</span></p>
          <p><strong>收货地址:</strong> {{ order.paymentAddress }}</p>
          <p><strong>创建时间:</strong> {{ order.createdAt }}</p>
        </div>
        <button @click="viewOrderDetails(order.id)">查看详情</button>
      </div>
    </div>

    <div v-else class="login-prompt">
      <p>请先登录查看您的订单</p>
    </div>
  </div>
</template>

<script setup>
/* 保持原有脚本部分完全不变 */
import { ref, onMounted } from 'vue'
import { isLoggedIn, currentUser } from '../stores/userStore.js'
import { useRouter } from 'vue-router'
import { getOrderByUserid } from '../api/order.js'

let orders = ref([])
const router = useRouter()

const viewOrderDetails = (orderId) => {
  router.push({ name: 'OrderDetail', params: { id: orderId } })
}

onMounted(async () => {
  if (isLoggedIn.value) {
    try {
      const userId = currentUser.value.userid;
      const response = await getOrderByUserid(userId);
      if (response.data.code === 1 && response.data.data) {
        orders.value = response.data.data.map(order => ({
          id: order.orderid,
          totalPrice: order.totalprice,
          paymentStatus: order.status,
          paymentAddress: order.shiping_address,
          createdAt: order.createtime
        }));
      }
    } catch (error) {
      console.error('Failed to fetch orders:', error);
    }
  }
})
</script>

<style scoped>
.order-container {
  padding: 20px;
  min-height: calc(100vh - 110px);
}

.user-orders {
  max-width: 800px;
  margin: 0 auto;
}

h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
}

.order-item {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.order-info p {
  margin: 12px 0;
  font-size: 15px;
  color: #666;
}

.order-info strong {
  color: #333;
  margin-right: 8px;
}

button {
  width: 100%;
  padding: 12px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.3s;
}

button:hover {
  background: #0056b3;
}

.status-tag {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 13px;
  background: #f0f0f0;
  color: #666;
}

.login-prompt {
  text-align: center;
  padding: 50px 20px;
  font-size: 16px;
  color: #666;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .order-container {
    padding: 15px;
  }

  h2 {
    font-size: 20px;
    margin-bottom: 20px;
  }

  .order-item {
    padding: 15px;
    margin-bottom: 15px;
  }

  .order-info p {
    font-size: 14px;
    margin: 10px 0;
  }

  button {
    padding: 10px;
    font-size: 14px;
  }

  .status-tag {
    font-size: 12px;
    padding: 3px 8px;
  }
}

@media (max-width: 480px) {
  .order-info p {
    font-size: 13px;
  }

  .order-info strong {
    display: block;
    margin-bottom: 4px;
  }
}
</style>